package com.hj.saapp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hj.saapp.R;
import com.hj.saapp.control.activity.MainActivity;
import com.hj.saapp.control.activity.SetingActivity;
import com.hj.saapp.control.activity.base.BaseActivity;

/**
 * Created by "丁永泽" on 2016/8/12.
 */

public class BaseView  extends LinearLayout{

    BaseActivity activity;
    MultiStateView multiStateView;
    TextView titleTv;
    TextView rightTv;


    public BaseView(Context context) {
        super(context, null);
        this.activity = (BaseActivity) context;
        initView();
    }

    public BaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView()
    {
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        LayoutParams titleParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        LayoutParams contentParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.setBackgroundResource(R.color.main_bk);
        this.setOrientation(VERTICAL);
        this.setLayoutParams(params);
        LayoutInflater inflater = LayoutInflater.from(activity);
        View title = inflater.inflate(R.layout.view_title,null);
        View content = inflater.inflate(activity.getLayoutByid(),null);
        multiStateView = new MultiStateView(activity);
        multiStateView.addView(content);

        Button lBtn = (Button) title.findViewById(R.id.lBtn);
        Button rBtn = (Button) title.findViewById(R.id.rBtn);


        this.addView(title,titleParams);
        LinearLayout linearLayout = (LinearLayout) title.findViewById(R.id.titleLeftLlay);
        LinearLayout rlayout = (LinearLayout) title.findViewById(R.id.rlayout);
        titleTv = (TextView) title.findViewById(R.id.title);
        rightTv = (TextView) title.findViewById(R.id.rightTv);
        RelativeLayout titleLeftRel = (RelativeLayout) title.findViewById(R.id.titleLeftRel);
        RelativeLayout rightNav = (RelativeLayout) title.findViewById(R.id.right_nav);

        if (this.activity instanceof MainActivity) {
            titleLeftRel.setVisibility(INVISIBLE);
            rightNav.setVisibility(VISIBLE);
            rBtn.setBackgroundResource(R.drawable.ic_reorder);
            rightNav.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    MainActivity mainActivity = (MainActivity) activity;
                    mainActivity.openDarw();
                }
            });


        }


        lBtn.setBackgroundResource(R.drawable.ic_action_arrow_left);


        if (activity instanceof SetingActivity) {
            linearLayout.setOnClickListener((OnClickListener) activity);
        } else {
            linearLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });

        }



        if (activity.getTitleById() != 0) {
            titleTv.setText(activity.getTitleById());
        }

        this.addView(multiStateView,contentParams);

    }

    public void  showLoading() {

        multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);

    }
    public void  showContent() {

        multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);

    }
    public void  showError() {

        multiStateView.setViewState(MultiStateView.VIEW_STATE_ERROR);

    }
    public void  showEmpty() {

        multiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);

    }
    public  void setTitle(int id) {
        titleTv.setText(id);
    }

}
